package repository

import (
	"fmt"
	"net/url"
	"wechat-push/config"
	"wechat-push/internal/model"
	"wechat-push/pkg/http"
	"wechat-push/pkg/logger"
)

const (
	regionUrl       = "https://geoapi.qweather.com/v2/city/lookup?location=%s&key=%s" // 获取地区编号
	currentWeather  = "https://devapi.qweather.com/v7/weather/now?location=%s&key=%s"
	forecastWeather = "https://devapi.qweather.com/v7/weather/3d?location=%s&key=%s"
	airUrl          = "https://devapi.qweather.com/v7/air/now?location=%s&key=%s"
)

func GetRegionCode(region string) (resp model.RegionCodeResp, err error) {
	reqUrl := fmt.Sprintf(regionUrl, url.QueryEscape(region), config.Cfg.QWeather.Key)
	err = http.Get(reqUrl, &resp)
	if err != nil {
		logger.Log.Error("http.Get err : ", err)
		return
	}
	return
}

func GetCurrentWeather(regionCode string) (resp model.CurrentWeatherResp, err error) {
	reqUrl := fmt.Sprintf(currentWeather, regionCode, config.Cfg.QWeather.Key)
	err = http.Get(reqUrl, &resp)
	if err != nil {
		logger.Log.Error("http.Get err : ", err)
		return
	}
	return
}

func GetForecastWeather(regionCode string) (resp model.ForecastWeatherResp, err error) {
	reqUrl := fmt.Sprintf(forecastWeather, regionCode, config.Cfg.QWeather.Key)
	err = http.Get(reqUrl, &resp)
	if err != nil {
		logger.Log.Error("http.Get err : ", err)
		return
	}
	return
}
func GetAirQuality(regionCode string) (resp model.AirResp, err error) {
	reqUrl := fmt.Sprintf(airUrl, regionCode, config.Cfg.QWeather.Key)
	err = http.Get(reqUrl, &resp)
	if err != nil {
		logger.Log.Error("http.Get err : ", err)
		return
	}
	return
}
